import pygame
import spyral
import math

import Dic
import BG;
import FG;
import Bird;
import Cannon;
import Power;
import Launch;
import Arrow;

class Game(spyral.scene.Scene):
    def __init__(self):
        spyral.scene.Scene.__init__(self)
        self.root_camera = spyral.director.get_camera()
        self.camera = self.root_camera.make_child(layers = [0,1,2,3])
        self.group = spyral.sprite.Group(self.camera)
        
        self.bg1 = BG.BG(1);
        self.bg2 = BG.BG(2);
        self.fg1 = FG.FG(1);
        self.fg2 = FG.FG(2);
        self.cannon = Cannon.Cannon()
        self.launch = Launch.Launch()
        self.bird = Bird.Bird(self.bg1, self.bg2, self.fg1, self.fg2, self.cannon);
        
        self.angle = 0;
        self.aiming = False;
        self.powering = False;
        self.arrow_on = False;
        
        self.group.add(self.bg1, self.bg2, self.fg1, self.fg2, self.bird, self.cannon, self.launch);
        
        
    def on_enter(self):
        bg = spyral.util.new_surface(Dic.geom['size'])
        bg.fill(Dic.colors['bg'])
        self.camera.set_background(bg)
                
    def render(self):
        self.group.draw()
        self.root_camera.draw()
        
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
                if event.key == pygame.K_r:
                    spyral.director.replace(Game())
            if (self.bird.launched == False):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if (self.launch.rect.collidepoint(pygame.mouse.get_pos()) == True):
                        self.setPower();
                    else:
                        self.aiming = True;
                elif event.type == pygame.MOUSEBUTTONUP:
                    if (self.powering == True):
                        self.bird.launch(self.power.getPercent() * 400, self.cannon.cannontipx, self.cannon.cannontipy)
                        self.group.remove(self.power)
                    self.aiming = False;
                    
        self.group.update()
        
        if (self.aiming == True):    
            self.angle = self.calcAngle();
            self.cannon.rotate(self.angle);
        
        if (self.bird.launched == False):
            self.bird.pos = (self.cannon.cannontipx-72, self.cannon.cannontipy-72)
        else:
            self.moveBG();
            
        if (self.bird.pos[1] < -2 * self.bird.img_h):
            if (self.arrow_on == False):
                self.arrow = Arrow.Arrow(self.bird);
                self.group.add(self.arrow);
                self.arrow_on = True;
        elif (self.arrow_on == True):
                self.group.remove(self.arrow)
                self.arrow_on = False;
            
            
        
    
    def moveBG(self):
        self.cannon.speed = self.bird.xvel;
        self.fg1.speed = self.bird.xvel;
        self.fg2.speed = self.bird.xvel;
        self.bg1.speed = self.bird.xvel / 8;
        self.bg2.speed = self.bird.xvel / 8;
    
    def calcAngle(self):
        mouse = pygame.mouse.get_pos();
        origin = (self.cannon.rect.center)
        if (mouse[1] <= origin[1] and origin[0] <= mouse[0]):
            hyp = (math.sqrt(math.pow(mouse[0] - origin[0], 2) + math.pow(mouse[1] - origin[1], 2)))
            adj = mouse[0] - origin[0]
            return math.degrees(math.acos(adj/hyp))
        elif (mouse[1] > origin[1]):
            return 0
        else:
            return 90;
        
    def setPower(self):
        self.group.remove(self.launch);
        self.power = Power.Power();
        self.group.add(self.power)
        self.powering = True;
        
        
        
        
        

